import cv2
import numpy as np

img = cv2.imread("../images/morph.png")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

img_canny = cv2.Canny(img_gray, 30, 70)

lines = cv2.HoughLinesP(
    img_canny,
    rho=1,  # 距离分辨率
    theta=np.pi / 180,   # 角度分辨率，通常为 np.pi / 180
    threshold=30,  # 累加器阈值   霍夫空间中交线的统计次数
    minLineLength=0,  # 最小线段长度, 小于该长度的线段会被抛弃
    maxLineGap=10,  # 同一行点之间的最大允许间隔，小于该间隔的点会被认为在同一条线段中
)

print(lines)
print(lines.shape)

hough_img = np.zeros_like(img, dtype=np.uint8)
for line in lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(hough_img, (x1, y1), (x2, y2), (0, 0, 255), 2)

cv2.imshow("Original Image", img)
cv2.imshow("Canny Image", img_canny)
cv2.imshow("Hough Lines", hough_img)
cv2.waitKey(0)